<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="loginForm.username" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="loginForm.password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
const FORMDATA_KEY = 'form_key'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        // 用户名
        username: '',
        // 密码
        password: '',
        // 记住我
        remember: false

      },
      // 记住我
      remember: false,

      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入用户名' },
          { min: 3, max: 10, trigger: 'blur', message: '长度在 3 到 10 个字符' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' },
          { min: 6, max: 15, trigger: 'blur', message: '长度在 6 到 15 个字符' }
        ]
        // remember: [
        //   {
        //     validator: (rule, value, callback) => {
        //       if (!value) {
        //         callback(new Error('请选择是否记住密码'))
        //       }
        //     }
        //   }
        // ]
      }
    }
  },
  created() {

  },
  mounted() {
    // 获取本地存储的表单数据
    const cacheFormStr = localStorage.getItem(FORMDATA_KEY)
    if (cacheFormStr) {
      // 获取数据
      const cacheFormData = JSON.parse(cacheFormStr)
      this.loginForm.username = cacheFormData.username
      this.loginForm.password = cacheFormData.password
      this.loginForm.remember = cacheFormData.remember
    }
  },
  methods: {
    async handleLogin() {
      // 校验表单
      this.$refs.loginForm.validate()
      // 存储Token
      // if (this.loginForm.remember) {
      //   await this.$store.dispatch('user/setToken', this.loginForm）
      // }
      const { username, password, remember } = this.loginForm
      await this.$store.dispatch('user/login', { username, password })
      this.$message.success('登录成功')
      this.$router.push('/')
      // 本地存储密码
      if (remember) {
        // 将表单数据存储到本地存储中
        localStorage.setItem(FORMDATA_KEY, JSON.stringify({ username, password, remember }))
      } else {
        // 删除本地存储
        localStorage.removeItem(FORMDATA_KEY)
      }
    }
  }

}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
